package cn.jsl.emp.controller;

import cn.jsl.basic.util.AjaxResult;
import cn.jsl.basic.util.PageList;
import cn.jsl.emp.domain.Employee;
import cn.jsl.emp.query.EmployeeQuery;
import cn.jsl.emp.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类: 员工
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工接口",description = "员工接口")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口：查询所有对象
     * @return
     */
    //路径:http://localhost:8080/employee + 无 + get
    @GetMapping
    @ApiOperation("查询所有对象")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //路径:http://localhost:8080/employee/1 + get
    @ApiOperation("查询单个对象")
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //路径:http://localhost:8080/employee + {json对象} + post
    @PostMapping
    @ApiOperation("分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    //路径:http://localhost:8080/employee/1 + delete
    @ApiOperation("删除对象")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param employee
     * @return
     */
    //路径:http://localhost:8080/employee + {json对象} + put
    @PutMapping
    @ApiOperation("添加或修改对象")
    public AjaxResult addAndUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除：接口
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 后端可以用数组也可以用集合
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    @GetMapping("/list/{shopId}")
    @ApiOperation("获取当前店铺的所有人")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }
}
